package net.ib.mtalk.global;

import net.ib.mntalk.protocol.Common.Calendar;
import net.ib.mntalk.protocol.Common.UUIDMsg;
import net.ib.mtalk.R;
import net.ib.mtalk.constant.PrefsConstant;
import net.ib.mtalk.util.GpbSupport;
import net.ib.mtalk.util.MTalkLogWriter;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class GlobalPreferences {

	private static SharedPreferences prefs = null;

	private static String dynamicDomain = null;
	private static int dynamicPort = -1;
	public static Boolean groupSort = false;
	private static UUIDMsg userUUID = null;
	private static UUIDMsg AppUUID = null;
	public static String registration_id = null;
	public static int registration_id_located = 0;
	private static UUIDMsg lastBuddyChangedNotiUuid = null;
	private static UUIDMsg lastViewChangedNotiUuid = null;
	private static UUIDMsg lastMessageReceivedNotiUuid = null;
	private static UUIDMsg lastReadmarkReceivedNotiUuid = null;
	private static String userName = null;
	private static String userStatus = null;
	private static Calendar userBirthday = null;
	private static long suggestionVersion = 0;
	//private static AccessToken accessToken = null;
	
	// 메모리에 적재
	public static UUIDMsg subscribeView = null;	
	
	public static String getDynamicDomain() {
		if (dynamicDomain == null) {
			dynamicDomain = prefs.getString(PrefsConstant.SESSION_SERVER_DOMAIN, null);
		}
		return dynamicDomain;
	}
	
	public static void setDynamicDomain(String domain) {
		dynamicDomain = domain;
		prefs.edit().putString(PrefsConstant.SESSION_SERVER_DOMAIN, dynamicDomain).commit();
	}

	public static int getDynamicPort() {
		if (dynamicPort == -1) {
			dynamicPort = prefs.getInt(PrefsConstant.SESSION_SERVER_PORT, -1);
		}
		return dynamicPort;
	}
	
	public static void setDynamicPort(int port) {
		dynamicPort = port;
		prefs.edit().putInt(PrefsConstant.SESSION_SERVER_PORT, dynamicPort).commit();
	}

	public static boolean getGroupSort(Context context) {
		if(groupSort != null) return groupSort;
		else{
			return groupSort;
		}
	}
	
	public static SharedPreferences getSharedPreferences() {
		// setSharedPreferences()가 GlobalApplication.onCreate()에서 호출되어 이 변수는 이미 세팅되었을 것이다.
		return prefs;
	}

	static void setSharedPreferences(Context myContext) {
		prefs = PreferenceManager.getDefaultSharedPreferences(myContext);
	}

	public static UUIDMsg getUserUUID() {
		if(userUUID == null) {
			userUUID = GpbSupport.makeUuidMsg(prefs.getLong(PrefsConstant.USER_UUID_MSB, 0), 
					prefs.getLong(PrefsConstant.USER_UUID_LSB, 0));
		} 
		return userUUID;
	}

	public static void setUserUUID(UUIDMsg newUserUUID) {
		if(newUserUUID != null) {
			long msb = GpbSupport.msbOf(newUserUUID);
			long lsb = GpbSupport.lsbOf(newUserUUID);
			MTalkLogWriter.d("SETTING USER_UUID : "+msb+", lsb:"+lsb);
			userUUID = newUserUUID;

			prefs.edit()
			.putLong(PrefsConstant.USER_UUID_MSB, msb)
			.putLong(PrefsConstant.USER_UUID_LSB, lsb)
			.commit();
		}
	}

	public static UUIDMsg getAppUUID() {
		if(AppUUID == null) {
			AppUUID = GpbSupport.makeUuidMsg(prefs.getLong(PrefsConstant.APP_UUID_MSB, 0), 
					prefs.getLong(PrefsConstant.APP_UUID_LSB, 0));
		}
		return AppUUID;
	}

	public static void setAppUUID(UUIDMsg newAppUUID) {
		if(newAppUUID != null) {
			long msb = GpbSupport.msbOf(newAppUUID);
			long lsb = GpbSupport.lsbOf(newAppUUID);
			MTalkLogWriter.d("SETTING APP_UUID : "+msb+", lsb:"+lsb);
			AppUUID = newAppUUID;

			prefs.edit()
			.putLong(PrefsConstant.APP_UUID_MSB, msb)
			.putLong(PrefsConstant.APP_UUID_LSB, lsb)
			.commit();
		}
	}

	public static void setRegistrationId(String id) {
		registration_id = id;
		prefs.edit().putString(PrefsConstant.REGISTRATION_ID, registration_id).commit();
	}

	public static String getRegistrationId() {
		if (registration_id == null) {
			registration_id = prefs.getString(PrefsConstant.REGISTRATION_ID, null);
		}
		return registration_id;
	}

	synchronized public static boolean isRegistrationIdLocated() {
		if (registration_id_located == 0) {
			registration_id_located = prefs.getInt(PrefsConstant.REGISTRATION_ID_LOCATED, 0);
		}
		if (registration_id_located > 0) {
			MTalkLogWriter.v("GlobalPreferences.isRegistrationIdLocated(): C2DM available");
		} else {
			MTalkLogWriter.v("GlobalPreferences.isRegistrationIdLocated(): C2DM inavailable");
		}
		return registration_id_located > 0;
	}

	synchronized public static void setRegistrationIdLocated(boolean isLocated) {
		if (isLocated) {
			registration_id_located = 1;
		} else {
			// negative가 0이라면, default와 일치하니 isRegistrationIdLocated()를 할 때마다 매번 읽어오게 된다.
			// 따라서 negative와 default를 구분하여 0일 때만 새로 읽어오도록.
			registration_id_located = -1;
		}
		if (! prefs.edit().putInt(PrefsConstant.REGISTRATION_ID_LOCATED, registration_id_located).commit()) {
			registration_id_located = 0;
		}
	}

	/**
	 * @return 노티를 위한 정보
	 */
	public static UUIDMsg getLastBuddyChangedNotiUuid() {
		if(lastBuddyChangedNotiUuid == null) {
			lastBuddyChangedNotiUuid = GpbSupport.makeUuidMsg(
					prefs.getLong(PrefsConstant.LAST_BUDDY_UUID_MSB, 0), 
					prefs.getLong(PrefsConstant.LAST_BUDDY_UUID_LSB, 0));
		}
		return lastBuddyChangedNotiUuid;
	}

	/**
	 * @param newLastViewUUID 노티를 위한 정보
	 */
	public static void setLastBuddyChangedNotiUUID(UUIDMsg newLastBuddyUuid) {
		if(newLastBuddyUuid != null) {
			lastBuddyChangedNotiUuid = newLastBuddyUuid;

			prefs.edit()
			.putLong(PrefsConstant.LAST_BUDDY_UUID_MSB, GpbSupport.msbOf(newLastBuddyUuid))
			.putLong(PrefsConstant.LAST_BUDDY_UUID_LSB, GpbSupport.lsbOf(newLastBuddyUuid))
			.commit();
		}
	}

	/**
	 * @return 노티를 위한 정보
	 */
	public static UUIDMsg getLastViewChangedNotiUuid() {
		if(lastViewChangedNotiUuid == null) {
			lastViewChangedNotiUuid = GpbSupport.makeUuidMsg(
					prefs.getLong(PrefsConstant.LAST_VIEW_UUID_MSB, 0), 
					prefs.getLong(PrefsConstant.LAST_VIEW_UUID_LSB, 0));
		}
		return lastViewChangedNotiUuid;
	}

	/**
	 * @param newLastViewUuid 노티를 위한 정보
	 */
	public static void setLastViewChangedNotiUUID(UUIDMsg newLastViewUuid) {
		if(newLastViewUuid != null) {
			lastViewChangedNotiUuid = newLastViewUuid;

			prefs.edit()
			.putLong(PrefsConstant.LAST_VIEW_UUID_MSB, GpbSupport.msbOf(newLastViewUuid))
			.putLong(PrefsConstant.LAST_VIEW_UUID_LSB, GpbSupport.lsbOf(newLastViewUuid))
			.commit();
		}
	}
	
	/**
	 * @return 노티를 위한 정보
	 */
	public static UUIDMsg getLastMessageReceivedNotiUuid() {
		if(lastMessageReceivedNotiUuid == null) {
			lastMessageReceivedNotiUuid = GpbSupport.makeUuidMsg(
					prefs.getLong(PrefsConstant.LAST_MESSAGE_UUID_MSB, 0), 
					prefs.getLong(PrefsConstant.LAST_MESSAGE_UUID_LSB, 0));
		}
		return lastMessageReceivedNotiUuid;
	}

	/**
	 * @param newLastViewUuid 노티를 위한 정보
	 */
	public static void setLastMessageReceivedNotiUUID(UUIDMsg newLastViewUuid) {
		if(newLastViewUuid != null) {
			lastMessageReceivedNotiUuid = newLastViewUuid;

			prefs.edit()
			.putLong(PrefsConstant.LAST_MESSAGE_UUID_MSB, GpbSupport.msbOf(newLastViewUuid))
			.putLong(PrefsConstant.LAST_MESSAGE_UUID_LSB, GpbSupport.lsbOf(newLastViewUuid))
			.commit();
		}
	}
	
	/**
	 * @return 노티를 위한 정보
	 */
	public static UUIDMsg getLastReadmarkReceivedNotiUuid() {
		if(lastReadmarkReceivedNotiUuid == null) {
			lastReadmarkReceivedNotiUuid = GpbSupport.makeUuidMsg(
					prefs.getLong(PrefsConstant.LAST_READMARK_UUID_MSB, 0), 
					prefs.getLong(PrefsConstant.LAST_READMARK_UUID_LSB, 0));
		}
		return lastReadmarkReceivedNotiUuid;
	}

	/**
	 * @param newLastViewUuid 노티를 위한 정보
	 */
	public static void setLastReadmarkReceivedNotiUUID(UUIDMsg newLastViewUuid) {
		if(newLastViewUuid != null) {
			lastReadmarkReceivedNotiUuid = newLastViewUuid;

			prefs.edit()
			.putLong(PrefsConstant.LAST_READMARK_UUID_MSB, GpbSupport.msbOf(newLastViewUuid))
			.putLong(PrefsConstant.LAST_READMARK_UUID_LSB, GpbSupport.lsbOf(newLastViewUuid))
			.commit();
		}
	}
	
	public static String getUserName() {
		if(userName == null){
			userName = prefs.getString(PrefsConstant.USER_NAME, " ");
		}
		return userName;
	}

	public static void setUserName(String setName) {
		if(setName != null){
			userName = setName;
			
			prefs.edit()
			.putString(PrefsConstant.USER_NAME, userName)
			.commit();
		}
	}

	public static String getUserStatus() {
		return userStatus;
	}

	public static void setUserStatus(String userStatus) {
		GlobalPreferences.userStatus = userStatus;
	}

	public static Calendar getUserBirthday() {
		return userBirthday;
	}

	public static void setUserBirthday(Calendar userBirthday) {
		GlobalPreferences.userBirthday = userBirthday;
	}

	public static boolean putMessageToPreferences(com.google.protobuf.micro.MessageMicro token) {
		return MessageUtils.putMessageToPreferences(token);
	}

	public static <T extends com.google.protobuf.micro.MessageMicro> T getMessageFromPreferences(Class<T> type) {
		return MessageUtils.getMessageFromPreferences(type);
	}

	public static int getBalloonColor()
	{
		return prefs.getInt(PrefsConstant.PRF_BALLOON_COLOR, R.drawable.ballon_default_blue);
	}

	public static int getBgColor()
	{
		return prefs.getInt(PrefsConstant.PRF_BG_COLOR, -1);
	}

	public static String getBgImageUriString()
	{
		return prefs.getString(PrefsConstant.PRF_BG_IMAGE_URI_STRING, null);
	}

	public static void setBalloonColor(int color)
	{
		GlobalPreferences.getSharedPreferences().edit().putInt(PrefsConstant.PRF_BALLOON_COLOR, color).commit();
	}

	public static void setBgColor(int color)
	{
		GlobalPreferences.getSharedPreferences().edit().putInt(PrefsConstant.PRF_BG_COLOR, color).commit();
	}

	public static void setBgImageUriString(String str)
	{
		GlobalPreferences.getSharedPreferences().edit().putString(PrefsConstant.PRF_BG_IMAGE_URI_STRING, str).commit();
	}

	public static boolean getBooleanReceiveConfirmChk()
	{
		return prefs.getBoolean(PrefsConstant.PRF_RECEIVE_CONFIRM_CHK, true);
	}

	public static void setBooleanReceiveConfirmChk(boolean bool)
	{
		GlobalPreferences.getSharedPreferences().edit().putBoolean(PrefsConstant.PRF_RECEIVE_CONFIRM_CHK, bool).commit();
	}

	public static boolean getBooleanLockChk()
	{
		return prefs.getBoolean(PrefsConstant.PRF_RECEIVE_LOCK_CHK, false);
	}

	public static void setBooleanLockChk(boolean bool)
	{
		GlobalPreferences.getSharedPreferences().edit().putBoolean(PrefsConstant.PRF_RECEIVE_LOCK_CHK, bool).commit();
	}
	
	public static boolean getLock()
	{
		return prefs.getBoolean(PrefsConstant.PRF_LOCK_CHK, false);
	}
	
	public static void setLock(boolean bool)
	{
		GlobalPreferences.getSharedPreferences().edit().putBoolean(PrefsConstant.PRF_LOCK_CHK, bool).commit();
	}
	
	public static String getLockPassWord()
	{
		return prefs.getString(PrefsConstant.PRF_LOCK_PW, null);
	}
	
	public static void setLockPassWord(String pw)
	{
		GlobalPreferences.getSharedPreferences().edit().putString(PrefsConstant.PRF_LOCK_PW, pw).commit();
	}
	
	public static void setContact_Version(int version){
		GlobalPreferences.getSharedPreferences().edit().putInt(PrefsConstant.PREF_CONTACT_VERSION, version).commit();
	}
	
	public static int getContact_Version(){
		return prefs.getInt(PrefsConstant.PREF_CONTACT_VERSION, 0);
	}
	
	public static void setMSISDN(String code){
		GlobalPreferences.getSharedPreferences().edit().putString(PrefsConstant.PREF_MSISDN, null).commit();
	}
	
	public static String getMSISDN(){
		return prefs.getString(PrefsConstant.PREF_MSISDN, "");
	}

	public static void setSuggestionVersion(long version){
		GlobalPreferences.getSharedPreferences().edit().putLong(PrefsConstant.PREF_SUGGESTIONVERSION, 0).commit();
	}
	
	public static long getSuggestionVersion(){
		return prefs.getLong(PrefsConstant.PREF_SUGGESTIONVERSION, 0);
	}
	
	public static boolean getBooleanAutoSync(Context context){
		return prefs.getBoolean(context.getString(R.string.preference_auto_sync_title), true);
	}
	
	public static boolean getBooleanNoti(Context context){
		return prefs.getBoolean(context.getString(R.string.preference_noti_title), true);
	}
	
	public static boolean getBooleanVibrate(Context context){
		return prefs.getBoolean(context.getString(R.string.preference_vibrate_title), true);
	}
	
	public static boolean getBooleanShowSender(Context context){
		return prefs.getBoolean(context.getString(R.string.preference_show_sender_title), true);
	}
	
	public static boolean getBooleanShowMessage(Context context){
		return prefs.getBoolean(context.getString(R.string.preference_show_message_title), true);
	}
	
	public static boolean getBooleanRepeatNoti(Context context){
		return prefs.getBoolean(context.getString(R.string.preference_repeat_noti_title), true);
	}
	
	public static boolean getBooleanFixDisplay(Context context){
		return prefs.getBoolean(context.getString(R.string.preference_display_chat_setting_title), false);
	}
	
	public static String getStringRingtone(Context context){
		return prefs.getString(context.getString(R.string.preference_ringtone_title), null);
	}
	
	public static void setSvcVersion(int ver)
	{
		GlobalPreferences.getSharedPreferences().edit().putInt(PrefsConstant.PRF_SVC_VERSION, ver).commit();
	}
	
	public static int getSvcVersion()
	{
		return prefs.getInt(PrefsConstant.PRF_SVC_VERSION, -1);
	}
	
	public static boolean getFixOrientation(Context context)
	{
		return prefs.getBoolean(context.getString(R.string.preference_display_chat_setting_title), false);
	}
}
